package com.chris.homecare.components.renderers
{
	import com.chris.homecare.TextureAtlasManager;
	import com.chris.homecare.utils.CommonUtils;
	
	import feathers.controls.Label;
	import feathers.controls.List;
	import feathers.controls.renderers.IListItemRenderer;
	import feathers.core.FeathersControl;
	
	import flash.geom.Point;
	
	import starling.display.Image;
	import starling.display.Quad;
	import starling.events.Event;
	import starling.events.Touch;
	import starling.events.TouchEvent;
	import starling.events.TouchPhase;
	
	public class ServicesListRenderer extends FeathersControl implements IListItemRenderer
	{
		protected var _data:Object;
		protected var _index:int = -1;
		protected var _owner:List;
		
		protected var _isSelected:Boolean = false;
		
		protected var _touchPointID:int = -1;
		private static const HELPER_TOUCHES_VECTOR:Vector.<Touch> = new <Touch>[];
		private static const HELPER_POINT:Point = new Point();
		private var background:Quad;
		private var bottomBorderQuad:Quad;
		private var topBorderQuad:Quad;
		
		private var serviceLabel:Label;
		private var arrowImg:Image;
		private var scaleValue:Number;
		
		public function ServicesListRenderer()
		{
			super();
			this.addEventListener(TouchEvent.TOUCH, touchHandler);
		}
		
		public function get data():Object
		{
			return this._data;
		}
		
		public function set data(value:Object):void
		{
			if(this._data == value)
			{
				return;
			}
			this._data = value;
			this.invalidate(INVALIDATION_FLAG_DATA);
		}
		
		public function get index():int
		{
			return this._index;
		}
		
		public function set index(value:int):void
		{
			this._index = value;
		}
		
		public function get owner():List
		{
			return List(this._owner);
		}
		
		public function set owner(value:List):void
		{
			if(this._owner == value)
			{
				return;
			}
			
			if(this._owner)
			{
				this._owner.removeEventListener(Event.SCROLL, owner_scrollHandler);
			}
			
			this._owner = value;
			
			if(this._owner)
			{
				this._owner.addEventListener(Event.SCROLL, owner_scrollHandler);
			}
			
			this.invalidate(INVALIDATION_FLAG_DATA);
		}
		
		public function get isSelected():Boolean
		{
			return this._isSelected;
		}
		
		public function set isSelected(value:Boolean):void
		{
			if(this._isSelected == value)
			{
				return;
			}
			
			this._isSelected = value;
			this.invalidate(INVALIDATION_FLAG_SELECTED);
			this.dispatchEventWith(Event.CHANGE);
		}
		
		override protected function initialize():void
		{
			this.background = new Quad(20, 20, 0xffffff);
			this.bottomBorderQuad = new Quad(10, 1, 0xe6e6e6);
			this.topBorderQuad = new Quad(10, 1, 0xe6e6e6);
			
			this.addChild(this.background);
			this.addChild(this.topBorderQuad);
			this.addChild(this.bottomBorderQuad);
			
			serviceLabel = new Label();
			serviceLabel.nameList.add("review-list-lbl");
			addChild(serviceLabel);
			
			arrowImg = new Image(TextureAtlasManager.getAtlas().getTexture("list-accessory-drill-down-icon"));
			addChild(arrowImg);
		}
		
		override protected function draw():void
		{
			super.draw();
			
			this.commitData();
			this.commitLayout();
		}
		
		protected function commitData():void
		{
			if(this._owner && this._data)
			{
				serviceLabel.text = this._data.header;
			}
				
			else
			{
				serviceLabel.text = "";
			}
		}
		
		protected function commitLayout():void
		{
			this.height = 88 * CommonUtils.scaleValue;
			
			this.topBorderQuad.width = this.background.width = this.bottomBorderQuad.width = this.width;
			this.background.height = this.height;
			this.bottomBorderQuad.y = this.height -1;
			
			arrowImg.height = arrowImg.height;
			arrowImg.width = arrowImg.width;
			
			arrowImg.x = this.width - 35;
			arrowImg.y = (this.height - arrowImg.height)/2;
				
			serviceLabel.x = 15*CommonUtils.scaleValue;
			serviceLabel.y = 30*CommonUtils.scaleValue;
			
			if(this._isSelected)
			{
				this.background.color = 0xDDDDDD;
			}
				
			else this.background.color = 0xFFFFFF;
			
			if(this._index != 0) this.topBorderQuad.visible = false;
		}
		
		
		protected function owner_scrollHandler(event:Event):void
		{
			this._touchPointID = -1;
		}
		
		protected function touchHandler(event:TouchEvent):void
		{
			if(!this._isEnabled)
			{
				return;
			}
			
			const touches:Vector.<Touch> = event.getTouches(this, null, HELPER_TOUCHES_VECTOR);
			if(touches.length == 0)
			{
				//end of hover
				return;
			}
			if(this._touchPointID >= 0)
			{
				var touch:Touch;
				for each(var currentTouch:Touch in touches)
				{
					if(currentTouch.id == this._touchPointID)
					{
						touch = currentTouch;
						break;
					}
				}
				
				if(!touch)
				{
					//end of hover
					HELPER_TOUCHES_VECTOR.length = 0;
					return;
				}
				
				if(touch.phase == TouchPhase.ENDED)
				{
					this._touchPointID = -1;
					touch.getLocation(this, HELPER_POINT);
					var isInBounds:Boolean = this.hitTest(HELPER_POINT, true) != null;
					if(isInBounds)
					{
						if(!this._isSelected)
						{
							this.isSelected = true;
						}
							
						else 
						{
							this.isSelected = false
						}
					}
				}
			}
			else //if we get here, we don't have a saved touch ID yet
			{
				for each(touch in touches)
				{
					if(touch.phase == TouchPhase.BEGAN)
					{
						this._touchPointID = touch.id;
						break;
					}
				}
			}
			HELPER_TOUCHES_VECTOR.length = 0;
		}
	}
}